package com.sh.sales.happysales.imgutil;

import android.content.Context;
import android.text.TextUtils;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

/**
 * Created by yuangang on 2016/5/12.
 */
public class LoadImageManager implements ILoadImageManager {
    private static LoadImageManager mInstance;
    protected ImageLoader imageLoader;
    private String Img_portalAddress;
    private String Poly_Img_portalAddress;
    public static final int IMG = 0;
    public static final int POLY_IMG = 1;

    public synchronized static LoadImageManager getInstance(Context mContext) {
        if (mInstance == null) {
            mInstance = new LoadImageManager(mContext);
        }
        return mInstance;
    }

    public LoadImageManager(Context mContext) {
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(mContext));
    }

    @Override
    public void setImgPortalAddress(String address) {
        this.Img_portalAddress = address;
    }

    @Override
    public void setPolyImgPortalAddress(String address) {
        this.Poly_Img_portalAddress = address;
    }

    @Override
    public void displayImage(String uri, ImageView imageView, int type, DisplayImageOptions options) {
        switch (type) {
            case IMG:
                if (!TextUtils.isEmpty(uri) && !uri.startsWith("http")) {
                    uri = this.Img_portalAddress + uri;
                }
                break;
            case POLY_IMG:
                if (!TextUtils.isEmpty(uri) && !uri.startsWith("http")) {
                    uri = this.Poly_Img_portalAddress + uri;
                }
                break;

            default:
                break;
        }
        imageLoader.displayImage(uri, imageView, options);
    }
}
